//
//  KYMineStatusController.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/10/16.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYMineStatusController.h"
#import "JSTInterestcircleDataModel.h"
#import "JSTMyDynamicCell.h"
#import "JSTCommentViewController.h"
#import "JSTInterestcircleDataFrame.h"

@interface KYMineStatusController ()<UITableViewDelegate, UITableViewDataSource,JSTMyDynamicCellDelegate,JSTAlertViewDelegate>

@property (nonatomic, strong) UITableView *tabView;

@property (nonatomic, strong) JSTInterestcircleDataModel *model;

@property (nonatomic, assign) NSInteger pageIndex;

@property (nonatomic, assign) NSInteger pageSize;

@property (nonatomic, strong) NSMutableArray *myDinamicData;

@property (nonatomic, strong) UIImageView *myDinamicFailImageView;

@end

@implementation KYMineStatusController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 0.设置标题&基本属性
    [self setUpTitleAndAttris];
    
    // 1.创建所有的子控件
    [self setUpAllSubViews];
}

#pragma mark - setUp
- (void)setUpAllSubViews
{
    [self.view addSubview:self.tabView];
    [self.tabView headerBeginRefreshing];
    [self.tabView addSubview:self.myDinamicFailImageView];
}

- (void)setUpTitleAndAttris
{
    // 0.分页数据
    _pageIndex = 1;
    _pageSize = 5;
    _myDinamicData = [NSMutableArray array];
}

#pragma mark - 请求数据
- (void)reloadOtherData
{
    _pageIndex++;
    [self requestDynamicData];
}

- (void)reloadRefresh
{
    _pageIndex = 1;
    [self requestDynamicData];
}

- (void)requestDynamicData
{
    NSString *pageSize = [NSString stringWithFormat:@"%ld", (long)_pageSize];
    NSString *pageIndex = [NSString stringWithFormat:@"%ld", (long)_pageIndex];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:pageSize forKey:@"pageSize"];
    [dict setValue:pageIndex forKey:@"page"];
    [JSTNewServer requestMyDynamic:dict success:^(NSDictionary *dataDic) {
        [self.tabView headerEndRefreshing];
        [self.tabView footerEndRefreshing];
        if (_pageIndex == 1) {
            [_myDinamicData removeAllObjects];
        }
        NSArray *arr = (NSArray *)dataDic;
        KYLog(@"---- dataDic = \n %@", dataDic);
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
             JSTInterestcircleDataModel *model = [[JSTInterestcircleDataModel alloc] init];
             model.timeText                      = [JSTSystemInfo getStr:[obj valueForKey:@"addtime"]];
             model.contentText                   = [JSTSystemInfo getStr:[obj valueForKey:@"content"]];
             model.contentImgUrlArr              = [obj valueForKey:@"imglist"];
             model.dynamicId                     = [JSTSystemInfo getStr:[obj valueForKey:@"id"]];
             model.pointPraise                   = [JSTSystemInfo getStr:[obj valueForKey:@"zan_count"]];
             model.commentNumber                 = [JSTSystemInfo getStr:[obj valueForKey:@"discuss_count"]];
             model.iconImgUrl                    = [JSTSystemInfo getStr:[obj valueForKey:@"icon"]];
             model.userName                      = [JSTSystemInfo getStr:[obj valueForKey:@"nickname"]];
             model.praiseStatus                  = [[JSTSystemInfo getStr:[obj valueForKey:@"is_zan"]] boolValue];
             [_myDinamicData addObject:model];
         }];
        
        if (IsArrEmpty(_myDinamicData)) {
            self.myDinamicFailImageView.hidden = NO;
        }
        [self.tabView reloadData];
        
    } error:^(NSString *errStr) {
         [self removeOverFlowActivityView];
         KYLog(@"---- errStr = %@", errStr);
         [self.tabView headerEndRefreshing];
         [self.tabView footerEndRefreshing];
        
         if (IsArrEmpty(_myDinamicData)) {
             self.myDinamicFailImageView.hidden = NO;
         }
    }];
}
#pragma mark - tableView数据源&代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _myDinamicData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JSTMyDynamicCell *cell = (JSTMyDynamicCell *)[self tableView:self.tabView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JSTMyDynamicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mineStatus"];
    if (!cell)
    {
        cell = [[JSTMyDynamicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mineStatus"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell createContent:[_myDinamicData safeObjectAtIndex:indexPath.section]];
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    JSTInterestcircleDataModel *model = [_myDinamicData safeObjectAtIndex:indexPath.section];
    [self onclickChangePage:model.dynamicId];
}

#pragma mark - method
/**
 *  根据对应点击的动态ID跳转详情界面
 */
- (void)onclickChangePage:(NSString *)dynamicId
{
    JSTCommentViewController *commentViewController = [[JSTCommentViewController alloc] init];
    commentViewController.dynamicId = dynamicId;
    [self.navigationController pushViewController:commentViewController animated:YES];
}

#pragma mark - JSTMyDynamicCellDelegate
/**
 跳转动态详情
 
 @param dynamicId 动态ID
 */
- (void)changeCommentController:(NSString *)dynamicId
{
    JSTCommentViewController *commentViewController = [[JSTCommentViewController alloc] init];
    commentViewController.dynamicId = dynamicId;
    [self.navigationController pushViewController:commentViewController animated:YES];
}

/**
 分享动态
 
 @param shareData 分享数据集合
 */
- (void)shareCurrentComment:(NSArray *)shareData
{
    [self onClickSocial:shareData];
}

#pragma mark - 分享
- (void)onClickSocial:(NSArray *)activityItems
{
    // 服务类型控制器
    UIActivityViewController *activityViewController =
    [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    
    [self presentViewController:activityViewController animated:YES completion:nil];
    
    // 选中分享类型
    [activityViewController setCompletionWithItemsHandler:^(NSString * __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError) {
        // 显示选中的分享类型
        KYLog(@"act type %@",activityType);
        
        if (completed)
        {
            NSLog(@"ok");
        }else {
            NSLog(@"no ok");
        }
    }];
}

/**
 点赞取消赞
 
 @param dynamicId 动态ID
 */
- (void)pointPraiseForCurrentComment:(NSString *)dynamicId
{
    JSTCommentViewController *commentViewController = [[JSTCommentViewController alloc] init];
    commentViewController.dynamicId = dynamicId;
    [self.navigationController pushViewController:commentViewController animated:YES];
}

/**
 删除动态
 
 @param dynamicId 动态ID
 */
- (void)deleteCurrentDynamic:(NSString *)dynamicId
{
    JSTAlertView *jstAlertView = [[JSTAlertView alloc] initWithTitle:@"" message:@"是否删除该动态！" customView:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
    [jstAlertView setConfirmBlock:^ {
        [self displayOverFlowActivityView];
        [JSTNewServer requestDeleteDynamic:dynamicId success:^(NSDictionary *dataDic) {
            [self requestDynamicData];
        } error:^(NSString *errStr) {
            [self removeOverFlowActivityView];
        }];
    }];
    [jstAlertView show];
}

#pragma mark - 懒加载界面
- (UITableView *)tabView
{
    if (!_tabView)
    {
        _tabView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tabView.delegate = self;
        _tabView.dataSource = self;
        _tabView.tableFooterView = [[UIView alloc] init];
        [_tabView addFooterWithTarget:self action:@selector(reloadOtherData)];
        [_tabView addHeaderWithTarget:self action:@selector(reloadRefresh)];
    }
    return _tabView;
}

- (UIImageView *)myDinamicFailImageView
{
    if (!_myDinamicFailImageView) {
        _myDinamicFailImageView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - kNewCurrentScreen(175))/2, kNewCurrentScreen(150), kNewCurrentScreen(175), kNewCurrentScreen(143))];
        _myDinamicFailImageView.image = [UIImage imageNamed:@"state_dynamic_bg"];
        _myDinamicFailImageView.hidden = YES;
    }
    return _myDinamicFailImageView;
}
@end
